﻿using LoveEatRabbit.API.Write.Applications.Commands.Users;
using LoveEatRabbit.Domain.RBAC;
using LoveEatRabbit.ErrorCode;
using LoveEatRabbit.Infrastructure.Interfaces;
using MD5Hash;
using MediatR;

namespace LoveEatRabbit.API.Write.Applications.CommandHandlers.Users
{
    public class UserUpdateCommandHandler : IRequestHandler<UserUpdateCommand, APIResult<int>>
    {
        private readonly IBaseRespository<UserModel> userRep;

        public UserUpdateCommandHandler(IBaseRespository<UserModel> userRep)
        {
            this.userRep = userRep;
        }

        public async Task<APIResult<int>> Handle(UserUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code=APIResultCode.修改成功;
            result.Message=APIResultCode.修改成功.ToString();

            var res=userRep.GetAll().FirstOrDefault(x=>x.UserId==request.UserId);
            res.UserPass=request.UserPass.GetMD5();
            res.UserNickname = request.UserNickname;
            res.UserName=request.UserName;
            res.RoleId=request.RoleId;
            await userRep.UpdateAsync(res);
            
            return await Task.FromResult(result);
        }
    }
}
